﻿// =================================================================== 
// 项目说明
//====================================================================
// 陕西龙源  @Copy Right 2006-2008
// 文件： SYS_PARAMETER.cs
// 项目名称：供电企业管理信息系统



// 创建时间：2010-03-19
// 负责人：
// ===================================================================
using System;
using System.Data;
using CommonLib.Model;
using CommonLib.IDAL;
using CommonLib.DALFactory;
using CommonLib.BLL;

namespace CommonLib.BLL
{
    /// <summary>
    ///   的BLL层



    /// </summary>
    public class SYS_PARAMETER : SYS_PARAMETERBLLBase
    {
		#region ----------变量定义----------
		#endregion
		
		#region ----------构造函数----------
		/// <summary>
		/// 默认构造函数



		/// </summary>
		public SYS_PARAMETER()
		{
		}
		#endregion
		
		#region----------函数定义---------

        #region	AddSysParam 

        public static int AddSysParam(CommonLib.Model.SYS_PARAMETER oSYS_PARAMETERInfo)
        {
            MyTransaction sp = new MyTransaction();
            sp.BeginTransaction();
            try
            {
                CommonLib.BLL.SYS_PARAMETER.Add(sp, oSYS_PARAMETERInfo);
                string[] deptIdArray = oSYS_PARAMETERInfo.DEPT_ID.Split(',');
                DataTable dt = CommonLib.BLL.SYS_PARAMETER_VALUES.GetList(" PARAMETER_ID ='" + oSYS_PARAMETERInfo.PARAMETER_ID + "'").Tables[0];
                foreach (DataRow dr in dt.Rows)
                {
                    CommonLib.BLL.SYS_PARAMETER_VALUES.Delete(sp, oSYS_PARAMETERInfo.PARAMETER_ID, dr["dept_id"].ToString());
                }
                if (deptIdArray != null)
                {
                    foreach (string s in deptIdArray)
                    {
                        CommonLib.Model.SYS_PARAMETER_VALUES oValues = new CommonLib.Model.SYS_PARAMETER_VALUES();
                        oValues.DEPT_ID = s;
                        oValues.PARAMETER_ID = oSYS_PARAMETERInfo.PARAMETER_ID;
                        oValues.PARAMETER_STR = oSYS_PARAMETERInfo.PARAMETER_STR;
                        CommonLib.BLL.SYS_PARAMETER_VALUES.Add(sp, oValues);
                    }
                }
                sp.CommitTran();
                return 0;
            }
            catch (System.Exception e)
            {
                sp.RollBackTran();
                throw (e);
                return -1;
            }
        }
        #endregion


        #region	UpdateSysParam 

        public static int UpdateSysParam(CommonLib.Model.SYS_PARAMETER oSYS_PARAMETERInfo,bool clsValue,bool deptUpdate)
        {
            MyTransaction sp = new MyTransaction();
            sp.BeginTransaction();
            try
            {
                CommonLib.BLL.SYS_PARAMETER.Update(sp, oSYS_PARAMETERInfo);
                if (clsValue)
                {
                    DataTable dt = CommonLib.BLL.SYS_PARAMETER_VALUES.GetList(" PARAMETER_ID ='" + oSYS_PARAMETERInfo.PARAMETER_ID + "'").Tables[0];
                    foreach (DataRow dr in dt.Rows)
                    {
                        CommonLib.BLL.SYS_PARAMETER_VALUES.Delete(sp, oSYS_PARAMETERInfo.PARAMETER_ID, dr["dept_id"].ToString());
                    }
                    string[] deptIdArray = null;
                    if (oSYS_PARAMETERInfo.DEPT_ID != "")
                    {
                        deptIdArray = oSYS_PARAMETERInfo.DEPT_ID.Split(',');
                        if (deptIdArray != null)
                        {
                            foreach (string s in deptIdArray)
                            {
                                CommonLib.Model.SYS_PARAMETER_VALUES oValues = new CommonLib.Model.SYS_PARAMETER_VALUES();
                                oValues.DEPT_ID = s;
                                oValues.PARAMETER_ID = oSYS_PARAMETERInfo.PARAMETER_ID;
                                oValues.PARAMETER_STR = oSYS_PARAMETERInfo.PARAMETER_STR;
                                CommonLib.BLL.SYS_PARAMETER_VALUES.Add(sp, oValues);
                            }
                        }
                    }
                }
                else
                {
                    if (deptUpdate)
                    {
                        
                        DataTable dt = CommonLib.BLL.SYS_PARAMETER_VALUES.GetList(" PARAMETER_ID ='" + oSYS_PARAMETERInfo.PARAMETER_ID + "'").Tables[0];
                        foreach (DataRow dr in dt.Rows)
                        {
                            if (oSYS_PARAMETERInfo.DEPT_ID.IndexOf(dr["dept_id"].ToString())==-1)
                                CommonLib.BLL.SYS_PARAMETER_VALUES.Delete(sp, oSYS_PARAMETERInfo.PARAMETER_ID, dr["dept_id"].ToString());
                        }

                        string [] deptIdArray = oSYS_PARAMETERInfo.DEPT_ID.Split(',');
                        if (deptIdArray != null)
                        {
                            foreach (string s in deptIdArray)
                            {
                                //if (CommonLib.BLL.SYS_PARAMETER_VALUES.GetRecorderCount(sp," PARAMETER_ID ='" + oSYS_PARAMETERInfo.PARAMETER_ID + "' and DEPT_ID ='" + s + "'") == 0)
                                {
                                    CommonLib.Model.SYS_PARAMETER_VALUES oValues = new CommonLib.Model.SYS_PARAMETER_VALUES();
                                    oValues.DEPT_ID = s;
                                    oValues.PARAMETER_ID = oSYS_PARAMETERInfo.PARAMETER_ID;
                                    oValues.PARAMETER_STR = oSYS_PARAMETERInfo.PARAMETER_STR;
                                    CommonLib.BLL.SYS_PARAMETER_VALUES.Add(sp, oValues);
                                }
                            }
                        }

                    }
                }
                sp.CommitTran();
                return 0;
            }
            catch (System.Exception e)
            {
                sp.RollBackTran();
                throw (e);
                return -1;
            }
        }
        #endregion

		#endregion
	}
}
